﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Abp.Auditing;
using Abp.UI;
using Abp.Web.Models;
using FwProject.Com;
using FwProject.Com.Dto;
using FwProject.Controllers;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;

namespace FwProject.Web.Shop.Controllers
{
    /// <summary>
    /// 供应商
    /// </summary>
    public class SupplierController : FwProjectControllerBase
    {
        private readonly IShopSupplierAppService _shopSupplierAppService;
        private readonly ISettlementSupplierAppService _settlementSupplierAppService;
        private readonly IShopOrderDetailsAppService _shopOrderDetailsAppService;
        private readonly IHostingEnvironment _env;
        public SupplierController(IShopSupplierAppService shopSupplierAppService, ISettlementSupplierAppService settlementSupplierAppService,
            IShopOrderDetailsAppService shopOrderDetailsAppService, IHostingEnvironment env)
        {
            _shopSupplierAppService = shopSupplierAppService;
            _settlementSupplierAppService = settlementSupplierAppService;
            _shopOrderDetailsAppService = shopOrderDetailsAppService;
            _env = env;
        }

        #region 供应商列表
        /// <summary>
        /// 供应商列表
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {
            return View();
        }


        [HttpGet]
        [DisableAuditing]
        [WrapResult(false, false)]//关闭ABP的返回数据封装
        public async Task<IActionResult> GetSuppliersData(int page, int limit, string keyword = "")
        {
            var data = await _shopSupplierAppService.GetSupplierLayerDataAsync(page, limit, keyword);
            return Json(data);
        }


        /// <summary>
        /// 供应商开启状态
        /// </summary>
        /// <param name="id"></param>
        /// <param name="active"></param>
        /// <returns></returns>
        public async Task<IActionResult> SwitchActive(Guid id, bool active)
        {
            await _shopSupplierAppService.SwitchActive(id, active);
            return Json(id);
        }


        public IActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public async Task<IActionResult> Create(ShopSupplierInput input)
        {
            var id = await _shopSupplierAppService.CreateShopSupplier(input);

            return Json(id);
        }

        public async Task<IActionResult> Edit(Guid id)
        {
            ViewBag.Id = id;
            var entity = await _shopSupplierAppService.GetEnterDtoAsync(id);
            return View(entity);
        }

        [HttpPost]
        public async Task<IActionResult> Edit(Guid id, ShopSupplierInput input)
        {
            await _shopSupplierAppService.EditShopAdLocation(id, input);
            return Json(id);
        }

        public async Task<IActionResult> Delete(Guid id)
        {
            await _shopSupplierAppService.DeleteAdLocation(id);
            return Json(id);
        }
        #endregion

        #region 供应商订单
        /// <summary>
        /// 供应商订单
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> Orders()
        {
            var list = await _shopSupplierAppService.GetListAsync();
            ViewBag.Supplier = list;
            return View();
        }

        [HttpGet]
        [DisableAuditing]
        [WrapResult(false, false)]
        public IActionResult GetSupplierOrdersData(int page,int limit,string supplier,string settlementStatus)
        {
            var model = _shopOrderDetailsAppService.GetSupplierOrdersData(page,limit,supplier,settlementStatus);
            return Json(model);
        }
        /// <summary>
        /// 订单结算
        /// </summary>
        public IActionResult Settlement(Guid supplierId,decimal totalPrice,string str)
        {
            if (str == "")
            {
                throw new UserFriendlyException("暂无未结算订单！");
            }
            ViewBag.SupplierId = supplierId;
            ViewBag.Totalprice = totalPrice.ToString("N");
            ViewBag.Str = str;
            return View();
        }
        [HttpPost]
        public async Task<IActionResult> Settlement(SettlementSupplierEnterDto input)
        {
            await _settlementSupplierAppService.EditSettlementStatus(input);
            return Json(1);
        }

        /// <summary>
        /// 导出
        /// </summary>
        public IActionResult ExportSupplierOrderData(string supplier, string settlementStatus)
        {
            var list = _shopOrderDetailsAppService.GetExportSupplierOrderData(supplier, settlementStatus);
            return DoExcel<SupplierOrdersDto>(list, _env);
        }
        #endregion
    }
}